// Copyright lowRISC contributors (OpenTitan project).
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

#ifndef OPENTITAN_SW_DEVICE_TESTS_PENETRATIONTESTS_JSON_PENTEST_LIB_COMMANDS_H_
#define OPENTITAN_SW_DEVICE_TESTS_PENETRATIONTESTS_JSON_PENTEST_LIB_COMMANDS_H_

#include "sw/device/lib/ujson/ujson_derive.h"
#ifdef __cplusplus
extern "C" {
#endif

#define MODULE_ID MAKE_MODULE_ID('j', 'p', 'c')

#define ALERT_HANDLER_PARAM_N_ALERTS 65
#define ALERT_HANDLER_PARAM_N_CLASSES 4
#define SENSOR_CTRL_PARAM_NUM_ALERT_EVENTS 11
#define ALERT_HANDLER_PARAM_N_PHASES 4

// clang-format off

#define PENETRATIONTEST_DEVICE_INFO(field, string) \
    field(device_id, uint32_t, 8) \
    field(rom_digest, uint32_t, 8) \
    field(icache_en, bool) \
    field(dummy_instr_en, bool) \
    field(clock_jitter_locked, bool) \
    field(clock_jitter_en, bool) \
    field(sram_main_readback_locked, bool) \
    field(sram_main_readback_en, bool) \
    field(sram_ret_readback_locked, bool) \
    field(sram_ret_readback_en, bool) \
    field(data_ind_timing_en, bool)
UJSON_SERDE_STRUCT(PenetrationtestDeviceInfo, penetrationtest_device_info_t, PENETRATIONTEST_DEVICE_INFO);

#define PENETRATIONTEST_CPUCTRL(field, string) \
    field(enable_icache, bool) \
    field(enable_dummy_instr, bool) \
    field(dummy_instr_count, uint32_t) \
    field(enable_jittery_clock, bool) \
    field(enable_sram_readback, bool) \
    field(enable_data_ind_timing, bool)
UJSON_SERDE_STRUCT(PenetrationtesCpuctrl, penetrationtest_cpuctrl_t, PENETRATIONTEST_CPUCTRL);

#define PENETRATIONTEST_SENSOR_CONFIG(field, string) \
    field(sensor_ctrl_enable, bool) \
    field(sensor_ctrl_en_fatal, bool, SENSOR_CTRL_PARAM_NUM_ALERT_EVENTS)
UJSON_SERDE_STRUCT(PenetrationtesSensorConfig, penetrationtest_sensor_config_t, PENETRATIONTEST_SENSOR_CONFIG);

#define PENETRATIONTEST_ALERT_CONFIG(field, string) \
    field(alert_classes, uint32_t, ALERT_HANDLER_PARAM_N_ALERTS) \
    field(enable_alerts, bool, ALERT_HANDLER_PARAM_N_ALERTS) \
    field(enable_classes, bool, ALERT_HANDLER_PARAM_N_CLASSES) \
    field(accumulation_thresholds, uint16_t, 4) \
    field(signals, uint32_t, 4) \
    field(duration_cycles, uint32_t, 4) \
    field(ping_timeout, uint32_t)
UJSON_SERDE_STRUCT(PenetrationtesAlertConfig, penetrationtest_alert_config_t, PENETRATIONTEST_ALERT_CONFIG);

#define PENETRATIONTEST_ALERTDUMP(field, string) \
    field(rst_reason, uint32_t) \
    field(alert_cause, bool, ALERT_HANDLER_PARAM_N_ALERTS) \
    field(loc_alert_cause, uint8_t) \
    field(class_accum_cnt, uint16_t, ALERT_HANDLER_PARAM_N_CLASSES) \
    field(class_esc_cnt, uint32_t, ALERT_HANDLER_PARAM_N_CLASSES) \
    field(class_esc_state, uint32_t, ALERT_HANDLER_PARAM_N_CLASSES)
UJSON_SERDE_STRUCT(PenetrationtesAlertDump, penetrationtest_alert_dump_t, PENETRATIONTEST_ALERTDUMP);

#define PENETRATIONTEST_OWNER_PAGE(field, string) \
    field(config_version, uint32_t) \
    field(sram_exec_mode, uint32_t) \
    field(ownership_key_alg, uint32_t) \
    field(update_mode, uint32_t) \
    field(min_security_version_bl0, uint32_t) \
    field(lock_constraint, uint32_t)
UJSON_SERDE_STRUCT(PenetrationtestOwnerPage, penetrationtest_owner_page_t, PENETRATIONTEST_OWNER_PAGE);

#define PENETRATIONTEST_BOOT_LOG(field, string) \
    field(digest, uint32_t, 8) \
    field(identifier, uint32_t) \
    field(scm_revision_low, uint32_t) \
    field(scm_revision_high, uint32_t) \
    field(rom_ext_slot, uint32_t) \
    field(rom_ext_major, uint32_t) \
    field(rom_ext_minor, uint32_t) \
    field(rom_ext_size, uint32_t) \
    field(bl0_slot, uint32_t) \
    field(ownership_state, uint32_t) \
    field(ownership_transfers, uint32_t) \
    field(rom_ext_min_sec_ver, uint32_t) \
    field(bl0_min_sec_ver, uint32_t) \
    field(primary_bl0_slot, uint32_t) \
    field(retention_ram_initialized, uint32_t)
UJSON_SERDE_STRUCT(PenetrationtestBootLog, penetrationtest_boot_log_t, PENETRATIONTEST_BOOT_LOG);

#define PENETRATIONTEST_BOOT_MEASUREMENTS(field, string) \
    field(bl0, uint32_t, 8) \
    field(rom_ext, uint32_t, 8)
UJSON_SERDE_STRUCT(PenetrationtestBootMeasurements, penetrationtest_boot_measurements_t, PENETRATIONTEST_BOOT_MEASUREMENTS);

#define PENETRATIONTEST_ALERT_CONFIG_OUT(field, string) \
    field(alert_classes, uint32_t, ALERT_HANDLER_PARAM_N_ALERTS) \
    field(enabled_alerts, bool, ALERT_HANDLER_PARAM_N_ALERTS) \
    field(enabled_classes, bool, ALERT_HANDLER_PARAM_N_CLASSES) \
    field(accumulation_thresholds, uint32_t, ALERT_HANDLER_PARAM_N_CLASSES) \
    field(duration_cycles, uint32_t, ALERT_HANDLER_PARAM_N_PHASES) \
    field(escalation_signals_en, bool, ALERT_HANDLER_PARAM_N_PHASES) \
    field(escalation_signals_map, uint32_t, ALERT_HANDLER_PARAM_N_PHASES)
UJSON_SERDE_STRUCT(PenetrationtestAlertConfigOut, penetrationtest_alert_config_out_t, PENETRATIONTEST_ALERT_CONFIG_OUT);

#define PENETRATIONTEST_SENSOR_CONFIG_OUT(field, string) \
    field(sensor_ctrl_en, bool, SENSOR_CTRL_PARAM_NUM_ALERT_EVENTS) \
    field(sensor_ctrl_fatal, bool, SENSOR_CTRL_PARAM_NUM_ALERT_EVENTS)
UJSON_SERDE_STRUCT(PenetrationtesSensorConfigOut, penetrationtest_sensor_config_out_t, PENETRATIONTEST_SENSOR_CONFIG_OUT);

#undef MODULE_ID

// clang-format on

#ifdef __cplusplus
}
#endif
#endif  // OPENTITAN_SW_DEVICE_TESTS_PENETRATIONTESTS_JSON_PENTEST_LIB_COMMANDS_H_
